<?php
use \Model\Queue;
use \Model\Account;
class Controller_Queue extends Controller_Weibo{
	public static $status = array(
		'to_send' => 0,
		'sent' => 1,
		'failed' => 2,
		'cancel' => 3,
		'del' => 4
	);

	public function action_portal() {
		$page_config = Config::get('pagination.queue', array());
		$page_config['total_items'] =Queue::get_cnt(Session::get('user_id', 0));
		$page = Uri::segment(2, 1);
		//$status = Input::get('status', 0);
		Config::set('pagination', $page_config);
		Pagination::$current_page = $page;
		$queue =Queue::get_queue(Session::get('user_id', 0), Pagination::$per_page, Pagination::$per_page * (Pagination::$current_page - 1));
		$page_links = Pagination:: create_links();
		$prev_link = Pagination::prev_link('prev');
		$next_link = Pagination::next_link('next');
		$stat = Queue::stat();
		$account = Account::get_account(Session::get('user_id', 0));
		$this->data['content'] = View::forge('queue/content', array('queue' => $queue, 'page_links' => $page_links, 'next_link' => $next_link, 'prev_link' => $prev_link, 'stat' => $stat, 'account' => $account));
		return Response::forge(View::forge('layout', $this->data));
	}

	public static function action_push_clone() {
		$data['text'] = Input::post('text', '');
		$data['screen_name'] = Input::post('screen_name', '');
		$data['image'] = Input::post('image', '');
		$data['post_time'] = date('Y-m-d H:i:s', strtotime(Input::post('post_time', '')));
		$data['user_id'] =Session::get('user_id', 0);
		$data['sina_id'] = Input::post('sina_id', '');
		$data['created_at'] = date('Y-m-d H:i:s', time() + 8 * 60 * 60);
		$data['profile_image_url'] = Account::get_profile_image_by_screen_name($data['screen_name']);
		Queue::add($data);
		return self::msg(array('msg'=>'succ'));
	}

	public static function action_upload() {
		$config = array(
				'path' => DOCROOT.'files',
				'randomize' => true,
				'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png')
		);

		// process the uploaded files in $_FILES
		Upload::process($config);

		// if there are any valid files
		if (Upload::is_valid())
		{
			// save them according to the config
			Upload::save();

			// call a model method to update the database
			$file = Upload::get_files();
			if (count($file) > 0) {
				return self::msg(array('msg'=>'succ', 'path'=>$file[0]['saved_to'].$file[0]['saved_as']));
			} else {
				return self::msg(array('msg'=>'fail', 'path'=>''));
			}
		}
	}

	public function action_action() {
		$action = Input::post('action', '');
		$queue_id = Input::post('queue_id', 0); 
		if (isset(self::$status[$action])) {
			$data['status'] = self::$status[$action];
			$data['id'] = $queue_id;
			Queue::update($data);
			return self::msg(array('msg'=>'succ'));
		}
		return self::msg(array('msg'=>'failed'));
	}

}
